package com.suray.wcs.res.msgparse;

import com.suray.basic.wcs.utils.Crc16;
import com.suray.wcs.res.msgparse.BitsObj;
import com.suray.wcs.res.msgparse.MsgTransferUtil;

/**
 * 发送地图信息
 *
 * @author Doofy&xuhang
 */
public class SendMsgMapData {
    private int index_w = 0;
    /**
     * 头帧  2字节
     */
    private final int firstFrame = 0x02fd;
    /**
     * 报文长度  2字节
     */
    private int msgLen;
    /**
     * 设备类型  1字节
     */
    private final int deviceType;
    /**
     * 设备号  1字节
     */
    private final int deviceNo;
    /**
     * 模式  1字节
     */
    private final int mode;
    /**
     * 地图  1字节
     */
    private final byte[] data;
    /**
     * 校验位  2字节
     */
    private int crc16;
    /**
     * 尾帧   2字节
     */
    private final int endFrame = 0x03fc;


    public SendMsgMapData(int deviceType, int deviceNo, int mode,byte[] data) {
        this.deviceType = deviceType;
        this.deviceNo = deviceNo;
        this.mode = mode;
        this.data = data;
    }


    byte[] addToByte(byte[] longBs, int value, int byteNum) {
        byte[] temp = MsgTransferUtil.addToByte(longBs, index_w, value, byteNum);
        index_w += byteNum;
        return temp;
    }

    byte[] addToByte(byte[] longBs, byte[] shortBs) {
        byte[] temp = MsgTransferUtil.addToByte(longBs, index_w, shortBs);
        index_w += shortBs.length;
        return temp;
    }

    public byte[] makeMsg() {
//			byte[] makeMsgWithoutXor = makeMsgWithoutXor();// 49个byte无校验位报文
//			int xorBits = getXorBits(makeMsgWithoutXor);
//			byte[] bs = makeMsgWithXor(xorBits);
//			return bs;
        byte[] bs = new byte[146];
        msgLen = bs.length;
        addToByte(bs, firstFrame, 2);
        addToByte(bs, msgLen, 2);
        addToByte(bs, deviceType, 1);
        addToByte(bs, deviceNo, 1);
        addToByte(bs, mode, 1);
        for (int i = 0; i < 133; i++) {
            addToByte(bs,data[i],1);
        }
        addToByte(bs, msgLen, 2);
        crc16 = Crc16.getCRC16(bs);
        addToByte(bs, crc16, 2);
        addToByte(bs, endFrame, 2);
        return bs;
    }


    public static void main(String[] args) {
        byte[] bs = new byte[0];
        System.out.println(bs.length);
    }

//		private byte[] makeMsgWithoutXor() {
//			byte[] bs = new byte[byteLengthWithoutXor];
//			setBits(bs, deviceNo);
//			setBits(bs, mode);
//			setBits(bs, mapVersion);
//			setBits(bs, driveCmdNo);
//			setBits(bs, srcLocation);
//			setBits(bs, dstLocation);
//			setBits(bs, cmdNo);
//			setBits(bs, cmd);
//			setBits(bs, cmdParam);
//			index_w = 0;
//			return bs;
//		}

    /**
     * 有校验位报文
     *
     * @param xorBits
     * @return
     */
//		private byte[] makeMsgWithXor(int xorBits) {
//			byte[] bs = new byte[byteLengthWithXor];
//			setBits(bs, deviceNo);
//			setBits(bs, mode);
//			setBits(bs, mapVersion);
//			setBits(bs, driveCmdNo);
//			setBits(bs, srcLocation);
//			setBits(bs, dstLocation);
//			setBits(bs, cmdNo);
//			setBits(bs, cmd);
//			setBits(bs, cmdParam);
//			MsgTransferUtil.setValues(bs, (bs.length*8-8), 8, xorBits);
//			index_w = 0;
//			return bs;
//		}

    /**
     * 计算异或校验值
     *
     * @param bs
     * @return
     */
    private int getXorBits(byte[] bs) {
        int xor = 0;
        for (int i = 0; i < bs.length; i++) {
            xor ^= bs[i];
        }
        return xor & 0xff;
    }

    /**
     * 修改值在byte数组里对应位的值
     *
     * @param bs
     * @param bitsObj
     */
    private void setBits(byte[] bs, BitsObj bitsObj) {
        int size = bitsObj.getSize();
        int value = bitsObj.getValue();
        MsgTransferUtil.setValues(bs, index_w, size, value);
        index_w += size;
    }


    public int getDeviceType() {
        return deviceType;
    }


    public int getDeviceNo() {
        return deviceNo;
    }


    public int getMode() {
        return mode;
    }


    public byte[] getData() {
        return data;
    }


}
